<style>
    .moad_inputbox input{ border:none;box-shadow:0 0 3px #ddd; border-radius:4px; text-align:right;font-family: 微软雅黑;font-size: 25px;font-weight: bold;color: rgb(102, 102, 102); width:100%; padding:5px 52px 5px 0}
    .moad_inputbox span{ position:absolute; right:15px; top:10px; line-height:25px;font-size:25px;color:#CCC}
    .page_paybox{background:#FFF; padding: 10px 0 0 0}
    .page_paybox .pp_left{ width:370px;padding:0 0 15px 15px; float:left; background:#FFF;}
    .page_paybox .pp_right{ width:360px;display:inline-block; background:#FFF;float:right;border-left:5px solid #EEE; padding-right:10px;}
    .page_paybox .pp_left ul{ width:100%; display:block}
    .page_paybox .pp_left ul li{ border-bottom:1px solid #EEE; padding:0; width:100%; display:-webkit-flex;display:flex; position:relative; margin-bottom: 5px}
    .page_paybox .pp_left ul li label{ font-size:17px; color:#999;font-weight:normal;width:110px; line-height:36px;}
    .page_paybox .pp_left ul li label b{ font-size:12px; font-weight:normal}
    .page_paybox .pp_left ul li input{ background:none; border:none; text-align:right; padding:0 10px; width:200px; font-size:29px; height:36px}
    .page_paybox .pp_left ul li span{width:30px; display:block; margin-top:10px; text-align:right; color:#999}
    .page_paybox .pp_left ul li.dis input{width:100px; color:#F66; }

    .ppr_btngroup {padding:5px 5px 5px 15px; margin-bottom:10px}
    .ppr_btngroup .layui-btn-group{ display:block;}
    .ppr_btngroup .layui-btn-group button{ width:25%;height:56px; line-height:56px;}
    .ppr_paybtngroup{position:relative;}
    .ppr_paybtngroup table{ width:100%; border:1px solid #CCC;}
    .ppr_paybtngroup table tr td{ border-right:1px solid #CCC; border-bottom:1px solid #CCC; text-align:center; height:56px; max-width:81.25px; cursor:pointer}
    .ppr_paybtngroup table tr td:last-child{ border-right:none;}
    .ppr_paybtngroup td.isselect{background:#31c17b;color:#FFF}
    .ppr_paybtngroup td.ischeck{background:#FF5722;color:#FFF}
    .ppr_paybtngroup button{ height:56px; line-height:56px;}
    .btn_paysure{background:#FFB800; color:#FFF;}
    .btn_paysure div{ font-size:60%;}
    .ppr_btndiscount div[class^='layui-col']{ text-align:center; border:1px solid #CCC; line-height:50px; cursor:pointer;}
    .ppr_btndiscount div[class^='layui-col']:nth-child(2){ border-left:none;border-right:none;}
    .ppr_btndiscount div[class^='layui-col'].isselect{background:#31c17b; color:#FFF;}
    .ppr_ctrl button.isselect{color:#288ad6}
    .ppl_memberbox{padding:0 10px 10px 10px; clear: left; background: #FFF;}
    .ppl_memberboxsearch{margin-bottom: 10px;}
    .ppl_memberboxinfo{border-radius: 4px;background: #5FB878;color: #FFF;  padding:10px;}
    .ppl_memberboxcard{padding:0 10px;}
    .ppl_memberboxcard ul li{height:80px; overflow: hidden;border-radius:4px;padding:10px 5px;border:1px solid #CCC; display: inline-block;margin-right:5px; cursor: pointer;}
    .ppl_memberboxcard ul li.isselect{background:#1E9FFF;color: #FFF}
</style>
<div class="modal_paycounter" index style="display:none;">
    <div class="page_paybox">
        <div class="pp_left">
            <form class="layui-form" id="form_paycounter" name="form_paycounter" action="">
                <ul>
                    <li>
                        <label>订单</label>
                        <input type="text" value="0" readonly id="ppl_input_orderfee" namn="ppl_input_orderfee"/>
                    </li>
                    <li class="dis">
                        <!--0无优惠,1系统满减，2手动折扣，3优惠券-->
                        <label id="ppl_dtype" type="0" default="">优惠</label>
                        <input type="text" value="100%" readonly  id="ppl_input_discount" namn="ppl_input_discount"/>
                        <input type="text" value="0" readonly  id="ppl_input_coupon" namn="ppl_input_coupon"/>
                        <span onclick="fun_ppRemove(2)"><i class="fa fa-remove-sign"></i></span> </li>
                    <li>
                        <label>应收</label>
                        <input type="text" value="0" readonly  id="ppl_input_totalfee" namn="ppl_input_totalfee"/>
                    </li>
                    <li>
                        <label style="color:#ff6a00" id="ppl_ptype">现金</label>
                        <input type="text" style="color:#ff6a00" value="0" maxlength="8" isselect="true" id="ppl_input_paidfee" onfocus='fun_changepaybox(this)' namn="ppl_input_paidfee" onclick="$(this).select();" oninput="fun_checkpaypress(this)"/>
                        <span onclick="fun_ppRemove(3)"><i class="fa fa-remove-sign"></i></span>
                    </li>
                    <li>
                        <label style="color:#ff6a00" id="ppl_ptype2">待收</label>
                        <input type="text" style="color:#ff6a00" value="0" maxlength="8" onfocus='fun_changepaybox(this)' readonly namn="ppl_input_paidfee2" id="ppl_input_paidfee2" onclick="if(!$(this).attr('readonly'))$(this).select();" oninput="fun_checkpaypress(this)"/>
                        <span onclick="fun_ppRemove(5)"><i class="fa fa-remove-sign"></i></span>
                    </li>
                    <li>
                        <label>找零</label>
                        <input type="text" value="0" readonly  id="ppl_input_change" namn="ppl_input_change"/>
                    </li>
                    <li>
                        <label>备注</label>
                        <input type="text" value=""  id="ppl_input_remark" namn="ppl_input_remark" onfocus='fun_changepaybox(this)' />
                        <span onclick="fun_ppRemove(4)"><i class="fa fa-remove-sign"></i></span> </li>
                </ul>
                <input type="hidden" name="ppl_paytype" id="ppl_paytype" value="2" />
                <input type="hidden" name="ppl_paytype2" id="ppl_paytype2" value="-1" />
                <input type="hidden" name="ppl_memberid" id="ppl_memberid"  />
                <input type="hidden" name="ppl_code" id="ppl_code" />
                <input type="hidden" name="ppl_qrcode" id="ppl_qrcode" />
                <input type="hidden" name="ppl_qrcode" id="ppl_paycode" />
                <input type="hidden" name="ppl_orderid" id="ppl_orderid" />
                <input type="hidden" name="pp_coupontype" id="pp_coupontype" />
                <input type="hidden" name="pp_couponno" id="pp_couponno" />
            </form>

        </div>
        <div class="pp_right">
            <div class="ppr_btngroup ppr_btndiscount">
                <div class="layui-row">
                    <div class="layui-col-xs4" onclick="fun_membersearch()">会员卡</div>
                    <div class="layui-col-xs4" onclick="fun_clearprice()">抹零</div>
                    <div class="layui-col-xs4" onclick="fun_inputAll()">全付</div>
                </div>
            </div>
            <div class="ppr_btngroup ppr_btndiscount">
                <div class="layui-row">
                    <div class="layui-col-xs4 counterCoupon" onclick="fun_counterCoupon(1)">手动改价</div>
                    <div class="layui-col-xs4 counterCoupon" onclick="fun_counterCoupon(2)">手动折扣</div>
                    <div class="layui-col-xs4 counterCoupon" onclick="fun_counterCoupon(3)">微信卡券</div>
                </div>
            </div>
            <div class="ppr_btngroup ppr_paybtngroup">
                <table >
                    <tbody>
                    <tr>
                        <td style="width:25%"  class="pprbtn isselect" paytype="2" onclick="fun_selectPaytype(2)"><div><i class="fa fa-ok"></i></div>现金</td>
                        <td style="width:25%"  class="pprbtn" paytype="3" onclick="fun_selectPaytype(3)">银行卡</td>
                        <td style="width:25%"  class="pprbtn" paytype="4" onclick="fun_selectPaytype(4)">会员卡</td>
                        <td style="width:25%" onclick="fun_grouppay(this)" id="isgrouppay" isgrouppay="0">组合支付</td>
                    </tr>
                    <tr>
                        {if !$cfg["isymf"] && !$cfg["isbank"] && !$cfg["appid"] && !$shop["appid"]}
                        <td style="width:25%"  class="pprbtn" paytype="0">-</td>
                        {else}
                        <td style="width:25%"  class="pprbtn" paytype="0" onclick="fun_selectPaytype(0)">微信</td>
                        {/if}
                        {if !$cfg["isymf"] && !$cfg["isbank"] && !$cfg["app_id"] && !$shop["app_id"]}
                        <td style="width:25%"  class="pprbtn" paytype="1">-</td>
                        {else}
                        <td style="width:25%"  class="pprbtn" paytype="1" onclick="fun_selectPaytype(1)">支付宝</td>
                        {/if}
                        {if $shop['custom_pay1_status']}
                        <td style="width:25%"  class="pprbtn" paytype="5" onclick="fun_selectPaytype(5)">{$shop['custom_pay1']}</td>
                        {else}
                        <td style="width:25%"  class="pprbtn">-</td>
                        {/if}
                        <td rowspan="2" style="width:25%" class="btn_paysure" onclick="fun_payPost();">确认
                            <div>结算完成</div></td>
                    </tr>
                    <tr>
                        {if $shop['custom_pay2_status']}
                        <td style="width:25%" class="pprbtn" paytype="6" onclick="fun_selectPaytype(6)">{$shop['custom_pay2']}</td>
                        {else}
                        <td style="width:25%"  class="pprbtn">-</td>
                        {/if}
                        {if $shop['custom_pay3_status']}
                        <td style="width:25%" class="pprbtn" paytype="7" onclick="fun_selectPaytype(7)">{$shop['custom_pay3']}</td>
                        {else}
                        <td style="width:25%"  class="pprbtn">-</td>
                        {/if}
                        {if $shop['custom_pay4_status']}
                        <td style="width:25%" class="pprbtn" paytype="8" onclick="fun_selectPaytype(8)">{$shop['custom_pay4']}</td>
                        {else}
                        <td style="width:25%"  class="pprbtn">-</td>
                        {/if}
                    </tr>
                    </tbody>
                </table>
            </div>
        </div>
        <div class="ppl_memberbox">
            <div class="layui-row">
                <div class="layui-col-md6">
                    <div class="ppl_memberboxinfo">
                        <p><span id="pplm_cardno"></span><span class="pull-right" onclick="fun_removeMember()"><i class="fa fa-remove"></i> </span></p>
                        <p id="pplm_name"></p>
                        <divi>
                            积分：<span id="pplm_credit"></span>
                            <span class="pull-right">余额：<span id="pplm_cash"></span></span>
                        </divi>
                    </div>
                </div>
                <div class="layui-col-md6">
                    <div class="ppl_memberboxcard">
                        <ul>
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>

</div>

<div class="modal_inputbox" index style="display:none;">
    <form index="" class="layui-form" action="" style="padding:15px">
        <Div class="text-center" id="modal_inputboximg" style="margin:20px 0;"><img src="{MODULE_URL}template/resources/img/scaner.png" /></Div>
        <div class="layui-form-item">
            <input type="text" name="modal_inputbox" id="modal_inputbox" lay-verify="required" placeholder="请输入名称" autocomplete="off" class="layui-input">
        </div>
        <div style="padding:10px; text-align:center" class="help-block"></div>
        <div class="row">
            <div class="col-md-12"><Div class="text-center"> <button class="layui-btn" id="modal_inputboxbtn" type="submit" >提交</button></Div></Div>
        </div>
    </form>
</div>
<script>
    function fun_checkpaypress(obj){
        var tempval=$(obj).val();
        var tempval=tempval.replace(/[^\d.]/g,'');
        tempval=tempval.replace(/\b(0+)/gi,"");
        if(isNaN(parseFloat(tempval)))tempval=0;
        $(obj).val(tempval);
        fun_paycount();
    }
    var payTypeAry=[2];
    function fun_btncount(num){
        var target=$(".page_paybox input[isselect='true']");
        txt="#"+target.attr("id");
        //alert(txt);
        if(parseInt(num)>10){
            target.val(num);
            fun_paycount();
        }else if(num=="D"){
            var temp=$("#ppl_input_totalfee").val().split(".");
            $("#ppl_input_totalfee").val(temp[0]+".00");
            fun_paycount();
            return false;
        }else{
            Counter(num,txt);
            fun_paycount();
        }
    }
    function fun_changepaybox(obj){
        $(".page_paybox input").removeAttr("isselect");
        $(obj).attr("isselect",true);
    }
    function fun_startPay(){
        if(arguments.length<1){
            fun_holdOrder(1);
            return;
        }
        var orderid=arguments[0];
        if(!orderid){
            layer.msg("请选择订单",{icon:2,time:1800});
            return;
        }
        var index2=layer.load(1, {shade: [0.1,'#fff']})
        layer.open({
            type:1,
            title:0,
            shade: [0.1,'#fff'],
            area:'760px',
            skin: 'layui-layer-rim',
            content:$(".modal_paycounter").show(),
            success: function(layero, index){
                $(".modal_paycounter").attr("zindex",index);
                payTypeAry=[];
                $(".pprbtn[paytype='2']").click();
                layui.use(['form'], function(){
                    var form = layui.form;
                    form.render();
                });
                $(".ppl_memberbox").hide();
                $(".ppl_memberboxcard ul").empty();
                $("#pplm_cardno").text("");
                $("#pplm_name").text("");
                $("#pplm_credit").text("");
                $("#pplm_cash").text("");
                $("#ppl_orderid").val(orderid);
                $.post("{php echo $this->createMobileUrl('index',array('op'=>'getorder'))}",{orderid:orderid},function(returnstr){
                    layer.close(index2);
                    console.log(returnstr);
                    var result=eval("("+returnstr+")");
                    if(result.success){
                        var trade=result.trade;
                        if(parseInt(trade.ispay)==1){
                            layer.close(index);
                            layer.msg("该订单已付款",{icon:2,time:1800});
                            return;
                        }
                        if(!parseInt(trade.order_fee)){
                            layer.close(index);
                            layer.msg("请先选择商品",{icon:2,time:1800});
                            return;
                        }
                        var orderfee=parseInt(trade.order_fee)*0.01;
                        $("#ppl_input_orderfee").val(orderfee.toFixed(2)).attr("default",orderfee.toFixed(2));
                        if(result.market){
                            var market=result.market;
                            $("#ppl_dtype").attr("type",1).attr("default",market.coupon).text("系统优惠");
                            $("#pp_coupontype").attr("mid",market.mid).val(1);
                            var disount=fee2num(market.coupon);
                            $("#ppl_input_coupon").val(disount);
                            $("#ppl_input_discount").val(parseInt(((orderfee-parseFloat(disount))/orderfee)*100)+"%");
                        }
                        fun_paycount();
                    }else{
                        layer.msg(result.msg,{icon:2,time:2000});
                        return;
                    }

                });
                $('#ppl_input_paidfee').focus();
            },
            end:function(){
                $("#ppl_input_orderfee").val('0.00');
                $("#ppl_input_teafee").val('0.00');
                $("#ppl_dtype").val(0);
                $("#ppl_dtype").attr("type","0").attr("default","");
                $("#ppl_input_discount").val('100%');
                $("#ppl_input_coupon").val('0.00');
                $("#ppl_input_totalfee").val('0.00');
                $("#ppl_input_paidfee").val('0');
                $("#ppl_input_paidfee2").val('0');
                $("#ppl_ptype").val(0);
                $("#ppl_ptype2").val("-1");
                $("#ppl_input_change").val('0.00');
                $("#ppl_ptype").text("现金");
                $("#ppl_input_remark").val('');
                $("#ppl_paytype").val(2);
                $("#ppl_memberid").val('');
                $("#ppl_code").val('');
                $("#ppl_paycode").val('');
                $("#ppl_qrcode").val('');
                $("#ppl_orderid").val('');
                $("#pp_coupontype").val('');
                $("#pp_couponno").val('');
                $("#ppl_txtrealname").text('');
                $("#ppl_txtmobile").text('');
                $("#ppl_txtlevel").text('');
                $("#ppl_txtdiscount").text('');
                $("#ppl_txtcredit").text('');
                $("#ppl_txtcash").text('');
                $("#ppl_txtcost").text('');
                $("#ppl_txtbirdday").text('');
                $(".ppr_btndiscount button").removeClass("isselect");
                $(".pprbtn").removeClass("isselect").find("div").remove();
                $(".modal_paycounter").hide();
                $("#isgrouppay").removeAttr("isgrouppay").removeClass("ischeck").text("组合支付");
            }
        });
    }

    function fun_clearprice(){
        var orderfee=str2num($("#ppl_input_orderfee").val());
        var totalfee=str2num($("#ppl_input_totalfee").val());
        var disount=orderfee-totalfee;
        console.log(disount);
        $("#ppl_input_coupon").val(fee2num(disount));
        $("#ppl_input_discount").val(parseInt(totalfee/orderfee*100)+"%");
        $(".counterCoupon").eq(0).addClass("isselect").siblings().removeClass("isselect");
        $("#ppl_dtype").attr("type",1);
        $("#ppl_dtype").text("手动折扣");
        $("#pp_coupontype").val(1);
        fun_paycount();
    }

    function fun_inputAll(){
        if($("#isgrouppay").attr("isgrouppay")=="1"){
            var total_fee=str2num($("#ppl_input_totalfee").val());
            var fee1=str2num($("#ppl_input_paidfee").val());
            var fee2=str2num($("#ppl_input_paidfee2").val());
            if(total_fee-fee1-fee2==0)return;
            if(fee2==0){
                $("#ppl_input_paidfee2").val(fee2num(total_fee-fee1));
            }else if(fee1==0){
                $("#ppl_input_paidfee").val(fee2num(total_fee-fee2));
            }
        }else{
            var total_fee=$("#ppl_input_totalfee").val();
            $("#ppl_input_paidfee").val(total_fee);
        }
        fun_paycount();
    }

    function fun_counterCoupon(type){
        var orderfee=parseFloat($("#ppl_input_orderfee").val());
        var totalfee=parseFloat($("#ppl_input_totalfee").val());
        var minDiscount=parseInt("{$shop['mindiscount']}");
        switch(type){
            case 1:
                fun_modal({
                    "title":"修改价格（0~"+orderfee+")",
                    "type":true,
                    "success":function(inputval,index){
                        if(isNaN(inputval)){
                            layer.msg("请输入数字",{icon:2,time:2000});
                            return;
                        }
                        if(parseFloat(inputval)>orderfee){
                            layer.msg("优惠金额不能大于订单金额",{icon:2,time:2000});
                            return;
                        }
                        if(minDiscount>0){
                            if(parseInt((parseFloat(inputval)/str2num(orderfee)))<minDiscount){
                                layer.msg("优惠金额不能大于"+minDiscount+"%",{icon:2,time:2000});
                                return;
                            }
                        }
                        layer.close(index);
                        var tempfee=parseFloat(inputval);
                        var disount=orderfee-tempfee;
                        $("#ppl_input_coupon").val(disount);
                        $("#ppl_input_discount").val(parseInt((parseFloat(inputval)/orderfee)*100)+"%");
                        $(".counterCoupon").eq(0).addClass("isselect").siblings().removeClass("isselect");
                        $("#ppl_dtype").attr("type",1);
                        $("#ppl_dtype").text("手动折扣");
                        $("#pp_coupontype").val(1);
                        fun_paycount();
                    }
                });
                break;
            case 2:
                fun_modal({
                    "title":"修改折扣（0~100)",
                    "type":true,
                    "success":function(inputval,index){
                        if(isNaN(inputval)){
                            layer.msg("请输入0-100之间的数字",{icon:2,time:2000});
                            return;
                        }
                        if(minDiscount>0){
                            if(parseInt(inputval)<minDiscount){
                                layer.msg("优惠金额不能小于"+minDiscount+"%",{icon:2,time:2000});
                                return;
                            }
                        }
                        layer.close(index);
                        var tempdis=100-parseInt(inputval);
                        var disount=(orderfee*tempdis*0.01).toFixed(2);
                        $("#ppl_input_coupon").val(disount);
                        $("#ppl_input_discount").val(parseInt(inputval)+"%");
                        $(".counterCoupon").eq(1).addClass("isselect").siblings().removeClass("isselect");
                        $("#ppl_dtype").attr("type",1).text("手动折扣");
                        fun_paycount();

                    }
                });
                break;
            case 3:
                layer.open({
                    type:1,
                    title:"使用微信优惠券",
                    shade: [0.1,'#fff'],
                    area:'300px',
                    skin: 'layui-layer-rim',
                    content:$(".modal_inputbox").show(),
                    success: function(layero, index){
                        $(document).unbind("keydown");
                        $("#modal_inputboximg").show();
                        $("#modal_inputboximg .help-block").text("请让客户打开卡包的中的优惠券");
                        $("#modal_inputbox").val('').attr("placeholder","请扫描/输入卡券号码");
                        $("#modal_inputbox").focus();
                        $("#modal_inputboxbtn").bind("click",function(){
                            var index2=layer.load(1, {shade: [0.1,'#fff']});
                            var cardno=$("#modal_inputbox").val();
                            $.post("{php echo $this->createMobileUrl('index',array('op'=>'checkcoupon'))}",{code:cardno},function(returnstr){
                                layer.close(index2);
                                layer.close(index);
                                console.log(returnstr);
                                var result=eval("("+returnstr+")");
                                if(result.success){
                                    var cardinfo=result.cardinfo;
                                    if(cardinfo.type!="discount" && cardinfo.type!="cash"){
                                        layer.msg("付款期间只能使用【折扣券/代金券】",{icon:2,time:2000});
                                        return;
                                    }
                                    var orderfee=parseFloat($("#ppl_input_orderfee").val());
                                    var _discount=parseInt(cardinfo['discount']);
                                    if(parseInt(cardinfo['least_cost']) && parseInt(cardinfo['least_cost'])>str2num(orderfee)){
                                        layer.msg("此优惠券必须满<b>"+(parseInt(cardinfo['least_cost'])*0.01).toFixed(2)+"</b>元才能使用哦",{icon:2,time:2000});
                                        return;
                                    }
                                    if(cardinfo.type=="discount"){
                                        var coupon=orderfee*(100-_discount)*0.01;
                                        $("#ppl_input_discount").val(_discount+"%");
                                        $("#ppl_input_coupon").val(coupon.toFixed(2));
                                    }else{
                                        var coupon=_discount*0.01;
                                        $("#ppl_input_discount").val(parseInt((orderfee-coupon)/str2num(orderfee))+"%");
                                        $("#ppl_input_coupon").val(coupon.toFixed(2));
                                    }
                                    $(".counterCoupon").eq(2).addClass("isselect").siblings().removeClass("isselect");
                                    $("#ppl_dtype").attr("type",3);
                                    $("#ppl_dtype").text("优惠券");
                                    $("#pp_coupontype").val(3);
                                    $("#pp_couponno").val(cardno);
                                    fun_paycount();
                                }else{
                                    layer.msg(result.msg,{icon:2,time:2000});
                                    return;
                                }

                            });
                            return false;
                        });
                    },
                    end:function(){
                        $(".modal_inputbox").hide();
                        $("#modal_inputboxbtn").unbind();
                    }
                });
                break;
        }
    }
    function fun_paycount(){
        var isgrouppay=parseInt($("#isgrouppay").attr("isgrouppay"));
        var orderfee=str2num($("#ppl_input_orderfee").val());
        var coupon=str2num($("#ppl_input_coupon").val());
        var total=orderfee-coupon;
        $("#ppl_input_totalfee").val((total*0.01).toFixed(2));
        var paidfee=str2num($("#ppl_input_paidfee").val());
        var change=0;
        if(isgrouppay){
            var paidfee2=str2num($("#ppl_input_paidfee2").val());
            var tmep1=total-paidfee-paidfee2;
            if(tmep1<0)tmep1=0;
            change=paidfee2+paidfee-total;
            $("#ppl_input_change").val((change*0.01).toFixed(2));
        }else{
            var tmep1=total-paidfee;
            if(tmep1<0)tmep1=0;
            change=paidfee-total;
            $("#ppl_input_paidfee2").val((tmep1*0.01).toFixed(2))
            $("#ppl_input_change").val((change*0.01).toFixed(2));
        }
        $("#print_iframe").attr("src","jetsumprint://order?event=price&price="+(total*0.01).toFixed(2));
    }
    function fun_ppRemove(type){
        /*清除优惠内容*/
        switch(type){
            case 2:
                var market=$("#ppl_dtype").attr("default");
                var orderfee=str2num($("#ppl_input_orderfee").val());
                if(market==""){
                    $("#ppl_input_coupon").val('0');
                    $("#ppl_input_discount").val("100%");
                }else{
                    if(market.indexOf("%")>-1){
                        market=parseInt(market.replace("%",""));
                        var tempdis=100-market;
                        var disount=(orderfee*tempdis*0.01).toFixed(2);
                        $("#ppl_input_coupon").val(disount);
                        $("#ppl_input_discount").val(parseInt(market)+"%");
                    }else{
                        var disount=parseInt(market);
                        $("#ppl_input_coupon").val((disount*0.01).toFixed(2));
                        $("#ppl_input_discount").val(parseInt(((orderfee-disount)/orderfee)*100)+"%");
                    }
                }
                $(".counterCoupon").removeClass("isselect");
                break;
            case 3:
                $("#ppl_input_paidfee").val('0');
                $("#ppl_input_paidfee").attr("isselect",true);
                $("#ppl_input_paidfee2").removeAttr("isselect");
                break;
            case 4:
                $("#ppl_input_remark").val('');
                break;
            case 5:
                if(parseInt($("#isgrouppay").attr("isgrouppay"))){
                    $("#ppl_input_paidfee2").val('0');
                    $("#ppl_input_paidfee2").attr("isselect",true);
                    $("#ppl_input_paidfee").removeAttr("isselect");
                }
                break;
        }
        fun_paycount();
    }

    function fun_grouppay(obj){
        var isgrouppay=parseInt($(obj).attr("isgrouppay"));
        isgrouppay = isgrouppay ? 0 : 1;
        $(obj).attr("isgrouppay",isgrouppay);
        if(isgrouppay){
            $("#ppl_input_paidfee2").removeAttr("readonly");

            $(obj).addClass("ischeck").html('<div><i class="fa fa-ok-sign"></i></div>组合支付');
        }else{
            if(payTypeAry.length>1){
                var temp=payTypeAry[1];
                payTypeAry=[];
                fun_selectPaytype(temp);
            }
            $("#ppl_input_paidfee2").attr("readonly",true);
            $(obj).removeClass("ischeck").html('组合支付');
        }
        fun_paycount();
    }
    function fun_selectPaytype(type){
        var isgrouppay=parseInt($("#isgrouppay").attr("isgrouppay"));
        for(var i in payTypeAry){
            if(payTypeAry[i]==type)return;
        }
        payTypeAry.push(type);
        if(isgrouppay){
            if(payTypeAry.length>2)payTypeAry.splice(0,1);
        }else{
            payTypeAry=[type];
        }
        if(payTypeAry.join("")=="01" || payTypeAry.join("")=="10"){
            var temp=payTypeAry[0];
            payTypeAry=[];
            fun_selectPaytype(temp);
            layer.msg("微信和支付宝不能同时使用！",{icon:2,time:2000});
            return;
        }
        $(".pprbtn").removeClass("isselect").find("div").remove();
        $("#ppl_ptype").text("现金");
        $("#ppl_ptype2").text("待收");
        $("#ppl_paytype").val("-1");
        $("#ppl_paytype2").val("-1");

        for(var i=0;i<payTypeAry.length;i++){
            var txt=$(".pprbtn[paytype='"+payTypeAry[i]+"']").text();
            if(i==0){
                $("#ppl_ptype").text(txt);
                $("#ppl_paytype").val(payTypeAry[i]);
            }else{
                $("#ppl_ptype2").text(txt);
                $("#ppl_paytype2").val(payTypeAry[i]);
            }
            $(".pprbtn[paytype='"+payTypeAry[i]+"']").addClass("isselect").html('<div><i class="fa fa-ok"></i></div>'+txt);
        }
        fun_paycount();
    }

    function fun_payPost(){
        var orderid=$("#ppl_orderid").val();
        var orderfee=$("#ppl_input_orderfee").val();
        var teafee=$("#ppl_input_teafee").val();
        var discount=$("#ppl_input_discount").val();
        var coupon=$("#ppl_input_coupon").val();
        var totalfee=$("#ppl_input_totalfee").val();
        var paidfee=$("#ppl_input_paidfee").val();
        var paidfee2=$("#ppl_input_paidfee2").val();
        var change=$("#ppl_input_change").val();
        var remark=$("#ppl_input_remark").val();
        var paytype=parseInt($("#ppl_paytype").val());
        var paytype2=parseInt($("#ppl_paytype2").val());
        var memberid=$("#ppl_memberid").val();
        var code=$("#ppl_code").val();
        var qrcode=$("#ppl_qrcode").val();
        var paycode=$("#ppl_paycode").val();
        var orderid=$("#ppl_orderid").val();
        var coupontype=$("#pp_coupontype").val();
        var couponno=$("#pp_couponno").val();
        var timecard=[];
        $(".ppl_memberboxcard .isselect").each(function(){
            timecard.push($(this).attr('gid'));
        });
        var data={
            "orderid":orderid,
            "orderfee":orderfee,
            "teafee":teafee,
            "discount":discount,
            "coupon":coupon,
            "totalfee":totalfee,
            "paidfee":paidfee,
            "paidfee2":paidfee2,
            "change":change,
            "remark":remark,
            "paytype":paytype,
            "paytype2":paytype2,
            "memberid":memberid,
            "code":code,
            "qrcode":qrcode,
            "paycode":paycode,
            "coupontype":coupontype,
            "couponno":couponno,
            "marketing":parseInt(coupontype)==1 ? $("#pp_coupontype").attr("mid") : 0,
            "timecard":timecard.join(),
        };
        if(parseInt($("#isgrouppay").attr("isgrouppay"))){
            if(str2num(paidfee)==0 || str2num(paidfee2)==0){
                layer.msg("组合支付必须同时有支付金额。",{icon:2,time:2000});
                return;
            }
            if(str2num(paidfee)+str2num(paidfee2)<str2num(totalfee)){
                layer.msg("付款金额不足",{icon:2,time:2000});
                return;
            }
            if(str2num(paidfee)+str2num(paidfee2)>str2num(totalfee)){
                if(parseInt(paytype)!=2 && parseInt(paytype2)!=2){
                    layer.msg("支付金额不能大于需要支付的金额",{icon:2,time:2000});
                    return;
                }
            }
        }else{
            if(str2num(paidfee)<str2num(totalfee)){
                layer.msg("付款金额不足",{icon:2,time:2000});
                return;
            }
        }
        if(paytype!=2 && paytype2!=2 && str2num(change)>0){
            layer.msg("总付款金额不能大于结算金额",{icon:2,time:2000});
            return;
        }
        if((paytype==0 || paytype==1) && !qrcode){
            fun_payneedInput(paytype);
            return;
        }
        if((paytype==3 || paytype2==3) && !code){
            fun_payneedInput(3);
            return;
        }
        if(paytype==4 || paytype2==4){
            if(!memberid){
                fun_payneedInput(4);
                return;
            }
            if(parseInt("{$cfg['ispaycode']}") && !paycode){
                fun_payneedInput(99);
                return;
            }
            var needpay= paytype ==4 ? str2num($("#ppl_input_paidfee").val()):str2num($("#ppl_input_paidfee2").val());
            var membercash=str2num($("#ppl_txtcash").val());
            if(needpay>membercash){
                layer.msg("余额不足，请先充值",{icon:2,time:2000});
                return;
            }
        }
        if((paytype2==0 || paytype2==1) && !qrcode){
            fun_payneedInput(paytype2);
            return;
        }
        console.log(data.change);
        var index2=layer.load(1, {shade: [0.1,'#fff']});
        $.post("{php echo $this->createMobileUrl('index',array('op'=>'payall'))}",data,function(returnstr){
            layer.close(index2);
            console.log(returnstr);
            var result=eval("("+returnstr+")");
            if(result.success){
                if(result.paywait){
                    var index3=layer.msg('等待用户输入支付密码',{icon:16,shade:0.1,time:35000});
                    setTimeout(function(){fun_payneedPass(orderid,index3,1)},5000);
                    return;
                }
                fun_paysuccess(orderid);
            }else{
                $("#ppl_qrcode").val("");
                $("#ppl_paycode").val("");
                layer.msg(result.msg,{icon:2,time:2000});
                return;
            }

        });
    }
    function fun_payneedInput(type){
        switch(type){
            case 3:
                layer.open({
                    type:1,
                    title:"使用银行卡支付",
                    shade: [0.1,'#fff'],
                    area:'400px',
                    skin: 'layui-layer-rim',
                    content:$(".modal_inputbox").show(),
                    success: function(layero, index){

                        $("#modal_inputboximg").hide();
                        $("#modal_inputboximg .help-block").text("请使用POS机收款后，再将银行卡后4位输入到上方");
                        $("#modal_inputbox").val('').attr("placeholder","请输入银行卡后4位");
                        $("#modal_inputbox").focus();
                        $("#modal_inputboxbtn").bind("click",function(){
                            layer.close(index);
                            $("#ppl_code").val($("#modal_inputbox").val());
                            fun_payPost();
                            return false;
                        });
                    },
                    end:function(){
                        $(".modal_inputbox").hide();
                        $("#modal_inputboxbtn").unbind();

                    }
                });
                break;
            case 4:
                if(!$("#ppl_memberid").val()){
                    layer.msg("请先选择会员卡",{icon:2,time:2000});
                    return;
                }
                var membercash=str2num($("#ppl_txtcash").text());
                var total=str2num($("#ppl_input_totalfee").val());
                if(membercash<total){
                    layer.msg("余额不足，请先充值",{icon:2,time:2000});
                    return;
                }
                fun_payPost();
                break;
            case 0:
                layer.open({
                    type:1,
                    title:"使用微信支付",
                    shade: [0.1,'#fff'],
                    area:['300px','480px'],
                    skin: 'layui-layer-rim',
                    content:$(".modal_inputbox").show(),
                    success: function(layero, index){

                        $("#modal_inputboximg").show();
                        $("#modal_inputboximg .help-block").text("请扫描客户的付款二维码");
                        $("#modal_inputbox").val('').attr("placeholder","请扫描客户的付款二维码");
                        $("#modal_inputbox").focus();
                        $("#modal_inputboxbtn").bind("click",function(){
                            layer.close(index);
                            $("#ppl_qrcode").val($("#modal_inputbox").val());
                            fun_payPost();
                            return false;
                        });
                    },
                    end:function(){
                        $(".modal_inputbox").hide();
                        $("#modal_inputboxbtn").unbind();

                    }
                });
                break;
            case 1:
                layer.open({
                    type:1,
                    title:"使用支付宝支付",
                    shade: [0.1,'#fff'],
                    area:['300px','480px'],
                    skin: 'layui-layer-rim',
                    content:$(".modal_inputbox").show(),
                    success: function(layero, index){

                        $("#modal_inputboximg").show();
                        $("#modal_inputboximg .help-block").text("请扫描客户的付款二维码");
                        $("#modal_inputbox").val('').attr("placeholder","请扫描客户的付款二维码");
                        $("#modal_inputbox").focus();
                        $("#modal_inputboxbtn").bind("click",function(){
                            layer.close(index);
                            $("#ppl_qrcode").val($("#modal_inputbox").val());
                            fun_payPost();
                            return false;
                        });
                    },
                    end:function(){
                        $(".modal_inputbox").hide();
                        $("#modal_inputboxbtn").unbind();

                    }
                });
                break;
            case 99:
                var index2=layer.load(1, {shade: [0.1,'#fff']});
                var memberid=$("#ppl_memberid").val();
                var orderid=$("#ppl_orderid").val();
                $.post("{php echo $this->createMobileUrl('index',array('op'=>'sendmemberpaycode'))}",{memberid:memberid,orderid:orderid},function(returnstr){
                    layer.close(index2);
                    console.log(returnstr);
                    var result=eval("("+returnstr+")");
                    if(result.success){
                        if(result.need){
                            layer.open({
                                type:1,
                                title:"请输入会员卡支付验证码",
                                shade: [0.1,'#fff'],
                                area:'400px',
                                skin: 'layui-layer-rim',
                                content:$(".modal_inputbox").show(),
                                success: function(layero, index){

                                    $("#modal_inputboximg").hide();
                                    $("#modal_inputboximg .help-block").text("请输入会员卡支付验证码");
                                    $("#modal_inputbox").val('').attr("placeholder","请输入会员卡支付验证码");
                                    $("#modal_inputbox").focus();
                                    $("#modal_inputboxbtn").bind("click",function(){
                                        layer.close(index);
                                        $("#ppl_paycode").val($("#modal_inputbox").val());
                                        fun_payPost();
                                        return false;
                                    });
                                },
                                end:function(){
                                    $(".modal_inputbox").hide();
                                    $("#modal_inputboxbtn").unbind();

                                }
                            });
                        }else {
                            $("#ppl_paycode").val(result.code);
                            fun_payPost();
                        }

                    }else{
                        layer.msg(result.msg,{icon:2,time:2000});
                        return;
                    }
                });

                break;
        }
    }
    function fun_payneedPass(orderid,indexs){
        var timers=1;
        if(arguments.length>2)timers=parseInt(arguments[2])+1;
        if(timers>=7){
            layer.close(indexs);
            layer.msg("支付时间超时",{icon:2,time:2000});
            return;
        }
        $.post("{php echo $this->createMobileUrl('index',array('op'=>'paywaitpassword'))}",{orderid:orderid},function(returnstr){
            console.log(returnstr);
            var result=eval("("+returnstr+")");
            if(result.success){
                if(result.paywait){
                    setTimeout(function(){fun_payneedPass(orderid,indexs,timers);},5000);
                    return;
                }
                fun_paysuccess(orderid);
            }else{
                layer.close(indexs);
                layer.msg(result.msg,{icon:2,time:2000});
                return;
            }
        });
    }
    function fun_paysuccess(orderid){
        layer.closeAll();
        layer.msg("收款成功",{icon:1,time:2000});
        var printpage=false;
        var printpage2=false;
        if($("#isprintform .layui-unselect").eq(0).hasClass("layui-form-checked"))printpage=true;
        var index2=layer.load(1,{shade: [0.1,'#fff']});
        $.post("{php echo $this->createMobileUrl('index',array('op'=>'paysuccess'))}",{orderid:orderid},function(returnstr){
            layer.close(index2);
            var result=eval("("+returnstr+")");
            if(result.success){
                if(printpage){
                    fun_print(1,orderid);
                }
            }else{
                layer.msg(result.msg,{icon:2,time:2000});
                return;
            }
            //---收款完成---//
        });
    }

    function fun_membersearch(){
        layer.open({
            type:1,
            title:"会员搜索",
            shade: [0.1,'#fff'],
            area:'300px',
            skin: 'layui-layer-rim',
            content:$(".modal_inputbox").show(),
            success: function(layero, index){
                $(document).unbind("keydown");
                $("#modal_inputboximg").show();
                $("#modal_inputboximg .help-block").text("请输入卡号/电话");
                $("#modal_inputbox").val('').attr("placeholder","卡号/电话");
                $("#modal_inputbox").focus();
                $("#modal_inputboxbtn").bind("click",function(){
                    var index2=layer.load(1, {shade: [0.1,'#fff']});
                    var keyword=$("#modal_inputbox").val();
                    var orderid=$("#ppl_orderid").val();
                    $.post("{php echo $this->createMobileUrl('index',array('op'=>'searchonemember'))}",{keyword:keyword,orderid:orderid},function(returnstr){
                        layer.close(index2);
                        console.log(returnstr);
                        var result=eval("("+returnstr+")");
                        if(result.success){
                            layer.close(index);
                            var member=result.member;
                            $("#ppl_memberid").val(member.id)
                            $(".ppl_memberbox").show();
                            $("#pplm_cardno").text(member.cardno ? member.cardno : member.wxcardno);
                            $("#pplm_name").text(member.realname);
                            $("#pplm_credit").text(member.credit2);
                            $("#pplm_cash").text(member.cash2);
                            /**/
                            $(".ppl_memberboxcard ul").empty();
                            var timecard=result.timecard;
                            if(timecard){
                                var temp='';
                                for(var i in timecard){
                                    temp+='<li class="isselect" onclick="chooseTimecard(this)" gid="'+timecard[i].id+'" fee="'+timecard[i].total_fee+'">'+(timecard[i].title).substring(0,4)+'<p>￥'+fee2num(timecard[i].total_fee)+'</p></li>';
                                }
                                $(".ppl_memberboxcard ul").append(temp);
                            }
                            fun_timecardCount();
                        }else{
                            layer.msg(result.msg,{icon:2,time:2000});
                            return;
                        }
                        return false;
                    });
                    return false;
                });
            },
            end:function(){
                $(".modal_inputbox").hide();
                $("#modal_inputboxbtn").unbind();
            }
        });
    }
    function fun_timecardCount(){
        var fee=0;
        $(".ppl_memberboxcard .isselect").each(function(){
            fee+=parseInt($(this).attr("fee"));
        });
        var total=str2num($("#ppl_input_orderfee").attr("default"));
        var showfee=total-fee;
        if(showfee<0)showfee=0;
        $("#ppl_input_orderfee").val(fee2num(showfee))
        fun_paycount();
    }
    function chooseTimecard(obj){
        $(obj).toggleClass("isselect");
        fun_timecardCount();
    }
    function fun_removeMember(){
        var orderid=$("#ppl_orderid").val();
        var indexs=$(".modal_paycounter").attr("zindex");
        layer.close(parseInt(indexs));
        layer.load(2,{time: 1500});
        setTimeout(function(){
            fun_startPay(orderid);
        },1500);
    }
</script>